/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.gui.composite;

import java.util.List;
import java.util.logging.Logger;

import  gnu.io.CommPortIdentifier;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;

import pl.vdl.azbest.annotations.InProgress;
import pl.vdl.azbest.annotations.Level;
import pl.vdl.azbest.annotations.TODO;
import pl.vdl.azbest.annotations.Team;
import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.mremote.Conf;
import pl.vdl.azbest.mremote.gui.GUIFacade;
import pl.vdl.azbest.mremote.gui.SWTElement;
//<strike></strike>
@InProgress
@TODO(
		dateAssigned="21-03-2008",
		level=Level.IMPORTANT,
		target="<br>" +
				"<strike>1. GUI for Setting essential communication parameters </strike><br>",
		done="1", responsible=Team.AZBEST	
)

public class CompositeComm extends Composite {
	{
		LoggingTheGathering.addPath(getClass().getName());
	}

	private Group group = null;
	private Label label = null;
	private Combo combo = null;
	private Label label1 = null;
	private Combo combo1 = null;
	private Logger logger = Logger.getLogger(getClass().getName());  //  @jve:decl-index=0:

	public CompositeComm(Composite parent, int style) {
		super(parent, style);
		initialize();
		init();
	}

	private void initialize() {
		createGroup();
		setSize(new Point(300, 200));
		setLayout(new GridLayout());
		this.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
			public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
				GUIFacade.getInstance().removeComposite(CompositeComm.this);
			}
		});
	}

	/**
	 * This method initializes group	
	 *
	 */
	private void createGroup() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group = new Group(this, SWT.NONE);
		group.setText(Conf.lang.getProperty("CompositeCom.group"));
		group.setLayout(gridLayout);
		label = new Label(group, SWT.NONE);
		label.setText(Conf.lang.getProperty("CompositeCom.label"));
		createCombo();
		label1 = new Label(group, SWT.NONE);
		label1.setText(Conf.lang.getProperty("CompositeCom.label1"));
		createCombo1();
	}

	/**
	 * This method initializes combo	
	 *
	 */
	private void createCombo() {
		combo = new Combo(group, SWT.READ_ONLY);
		combo
		.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
			public void widgetSelected(
					org.eclipse.swt.events.SelectionEvent e) {
				//System.out.println("widgetSelected()");  // TODO
				// Auto-generated
				// Event stub
				// widgetSelected()
				Conf.getInstance().setDevicePort(
						Conf.getInstance().getAvailableCommPorts().get(
								combo.getSelectionIndex()));
			}

			public void widgetDefaultSelected(
					org.eclipse.swt.events.SelectionEvent e) {
			}
		});
	}

	/**
	 * This method initializes combo1	
	 *
	 */
	private void createCombo1() {
		combo1 = new Combo(group, SWT.READ_ONLY);
		combo1.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				logger.info(""+combo1.getSelectionIndex()+" rate " +Conf.getInstance().getAvailableBaudRates()[combo1.getSelectionIndex()]);
				Conf.getInstance().setDeviceBaudRate(Conf.getInstance().getAvailableBaudRates()[combo1.getSelectionIndex()]);
				logger.info("Baud rate set : "+Conf.getInstance().getDeviceBaudRate());
			}
			public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
			}
		});
	}
	
	
	private void init() {
		List<CommPortIdentifier> list = Conf.getInstance()
				.getAvailableCommPorts();
		for (CommPortIdentifier id : list) {
			combo.add(id.getName());
		}
		combo.select(0);
		for(int rate :Conf.getInstance().getAvailableBaudRates()){
			combo1.add(Integer.toString(rate));
		}
		combo1.select(0);
		
		GUIFacade.getInstance().addComposite(this);
	}
	
	private SWTElementInner  swtEi = null;
	class SWTElementInner extends SWTElement{
		SWTElementInner(Shell s){
			this.sShell=s;
		}
		@Override
		protected void createSShell(){}
	}


}  //  @jve:decl-index=0:visual-constraint="110,31"
